package cn.jiyun.controller;

import cn.jiyun.service.UserService;
import cn.jiyun.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("ccc")
public class UserController {
    @Autowired
    private UserService userService;

    /*登录*/
    @RequestMapping("/*")
    public String showRegister(){
        return "login";
    }

    /*登录判断*/
    @RequestMapping("logins")
    public String logins(Model model, User users, HttpSession session) {
        User user = userService.login(users);
        if(user!=null){
            session.setAttribute("user", user);
            return "redirect:findAll";
        }
        else {
            model.addAttribute("err", "账号或年龄错误！！");
            return "login";
        }
    }
    /*展示*/
    @RequestMapping("findAll")
    public String findAll(Model model){
        List<User> list = userService.findAll();
        model.addAttribute("list",list);
        return "show";
    }
    /*添加页面*/
    @RequestMapping("toadd")
    public String toadd()
    {
        return "add";
    }

    /**
     * 添加用户
     */
    @RequestMapping("add")
    public String addUser(User user) {
        this.userService.add(user);
        return "redirect:findAll";
    }
    /*修改*/
    @RequestMapping("update")
    public String update(User user)
    {
        userService.update(user);

        return "redirect:findAll";
    }

    /*修改回显*/
    @RequestMapping("findupdate")
    public String findupdate(Integer id,Model model){
        User user = userService.findUpdate(id);
        model.addAttribute("user",user);
        return "update";
    }
    /*删除*/
    @RequestMapping("delete")
    public String delete(Integer id){
        userService.del(id);
        return "redirect:findAll";/*重定向:redirect     转发:forword*/
    }
}
